package com.itheima.controller;


import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    //查找服务
    @Autowired
    private RoleService roleService;

    //分页查询
    @RequestMapping("/pageQuery")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.pageQuery(queryPageBean);
        return pageResult;
    }


    //新增一个角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleService.add(role, permissionIds, menuIds);
        return Result.success(MessageConstant.ADD_ROLE_SUCCESS);
    }

    //编辑一个角色,用于权限数据的回显,通过角色的id查找对应的角色的权限
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer id) {
        List<Integer> data = roleService.findPermissionIdsByRoleId(id);
        return Result.success(data, MessageConstant.QUERY_PERMISSION_SUCCESS);
    }

    //编辑一个角色,用于菜单数据的回显,通过角色的id查找对应的角色的菜单
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer id) {
        List<Integer> data = roleService.findMenuIdsByRoleId(id);
        return Result.success(data, MessageConstant.QUERY_MENU_SUCCESS);
    }


    //编辑一个角色,用于角色的基本数据的回显
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Role data = roleService.findById(id);
        return Result.success(data, MessageConstant.QUERY_ROLE_SUCCESS);
    }

    //编辑一个角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleService.edit(role, permissionIds, menuIds);
        return  Result.success(MessageConstant.EDIT_ROLE_SUCCESS);
    }

    //删除一个角色
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        roleService.delete(id);
        return Result.success(MessageConstant.DELETE_ROLE_SUCCESS);
    }


    //查询所有的角色
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Role> data = roleService.findAll();
        return Result.success( data,MessageConstant.QUERY_ROLE_SUCCESS );
    }
}
